package com.water.camera.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.water.camera.bean.MediaData;
import com.water.camera.databinding.FragmentImgBinding;

public class ImgFragment extends BaseFragment {

    MediaData data;
    String path;
    FragmentImgBinding binding;

    public void setPath(MediaData img) {
        this.data = img;
    }

    public void setPath(String p) {
        path = p;
        if (binding != null && isAdded()) {
            try {
                Glide.with(getActivity())
                        .setDefaultRequestOptions(new RequestOptions())
                        .load(path)
                        .skipMemoryCache(true)
//                    .override(data.getWidth(), data.getHeight())
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(binding.img);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentImgBinding.inflate(inflater, container, false);
        if (data != null) {
            Glide.with(getActivity())
                    .setDefaultRequestOptions(new RequestOptions())
                    .load(data.getPath())
                    .skipMemoryCache(true)
//                    .override(data.getWidth(), data.getHeight())
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(binding.img);
//            Glide.with(this).load(f).signature(new ObjectKey(f.lastModified())).into(img);
        } else if (!TextUtils.isEmpty(path)) {
            Glide.with(getActivity())
                    .setDefaultRequestOptions(new RequestOptions())
                    .load(path)
                    .skipMemoryCache(true)
//                    .override(data.getWidth(), data.getHeight())
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(binding.img);
//            Glide.with(this).load(f).signature(new ObjectKey(f.lastModified())).into(img);
        }
        return binding.getRoot();
    }
}
